﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics.Contracts;
using System.Security;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Diagnostics;

namespace MetaTracker.Model
{
    internal partial class TrackerModel
    {

        [Pure]
        private DateTime GetStartOfWeek(DateTime date)
        {
            DateTime startOfWeek;

            switch (date.DayOfWeek)
            {
                case DayOfWeek.Sunday:
                    startOfWeek = date;
                    break;
                case DayOfWeek.Monday:
                    startOfWeek = date.AddDays(-1.0);
                    break;
                case DayOfWeek.Tuesday:
                    startOfWeek = date.AddDays(-2.0);
                    break;
                case DayOfWeek.Wednesday:
                    startOfWeek = date.AddDays(-3.0);
                    break;
                case DayOfWeek.Thursday:
                    startOfWeek = date.AddDays(-4.0);
                    break;
                case DayOfWeek.Friday:
                    startOfWeek = date.AddDays(-5.0);
                    break;
                case DayOfWeek.Saturday:
                    startOfWeek = date.AddDays(-6.0);
                    break;
                default:
                    throw new ArgumentOutOfRangeException("Argument is apparently not a day of the week.");
            }

            return startOfWeek;
        }


        [Pure]
        public static string SecureStringToSHA1(SecureString password)
        {
            //http://stackoverflow.com/questions/1529487/how-do-a-use-a-securestring-to-create-a-sha1-or-sha512-hash
            int passwordLength = password.Length;
            char[] passwordChars = new char[passwordLength];

            // Copy the password from SecureString to our char array
            IntPtr passwortPointer = Marshal.SecureStringToBSTR(password);
            Marshal.Copy(passwortPointer, passwordChars, 0, passwordLength);
            Marshal.ZeroFreeBSTR(passwortPointer);

            //// Hash the char array
            //MD5 md5Hasher = MD5.Create();
            //byte[] hashedPasswordBytes = md5Hasher.ComputeHash(Encoding.Default.GetBytes(passwordChars));

            string rethash = "";
            try
            {
                SHA1 hash = SHA1.Create();
                ASCIIEncoding encoder = new ASCIIEncoding();
                hash.ComputeHash(Encoding.Default.GetBytes(passwordChars));
                rethash = Convert.ToBase64String(hash.Hash);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Error in HashCode: " + ex.Message);
            }

            // Wipe the character array from memory
            for (int i = 0; i < passwordChars.Length; i++)
            {
                passwordChars[i] = '\0';
            }

            // Return the result
            return rethash;
        }

    }
}
